---
import Layout from '../components/Layout.astro';
---
<Layout>
	<p id="name">Keep Style</p>
	<a id="click" href="/keep-two">go to next page</a>
</Layout>
<script>

	import { deselectScripts, saveFocus, swapBodyElement, swapHeadElements, swapRootAttributes } from '../../node_modules/astro/dist/transitions/swap-functions'

	document.addEventListener('astro:before-swap', (e) => {
		e.swap = () => keepStyle(e.newDocument)
	});

	function keepStyle(doc: Document) {
		deselectScripts(doc);
		swapRootAttributes(doc);
		{
			const dynamicStyle = document.head.querySelector('style:not(:empty)');
			swapHeadElements(doc);
			dynamicStyle && document.head.insertAdjacentElement('afterbegin', dynamicStyle);
		}
		const restoreFocusFunction = saveFocus();
		swapBodyElement(doc.body, document.body)
		restoreFocusFunction();
	}
</script>
